
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  pool.preprocess;

import java.lang.reflect.Method;
import java.util.Hashtable;

import logger.PoolLogger;
import logger.ProcessLogger;

import pool.tablename.Pool;
import utility.CapitalChar;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;


public class Preprocess 
{
	GetIdName gid;
	String PROCESS="process";
	String PREPROCESS ="preprocess";
	String TYPE="type";
	Object object;
	Pool pool;
	Hashtable<Object, Object>table = new Hashtable<Object, Object>();
	Hashtable<Object, Object>resulttable = new Hashtable<Object, Object>();
	PoolLogger pl;
	
	public  void initPool() 
	{
		gid = new  GetIdName();
		PROCESS = gid.getId(PROCESS);
		PREPROCESS = gid.getId(PREPROCESS);
		TYPE = gid.getId(TYPE);
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public void ProcessPreprocess()
	{	
		String preprocess = (pool.getType());
		String tablename = pool.getTable();
		Hashtable<Object,Object> table = new Hashtable<Object,Object> ();
		table.put("pid",PROCESS);
		table.put("mid", preprocess);
		table.put("td", "null");
		RecordSearch ps = new RecordSearch();
		ps.setConditionMap(table);
		String line[] = ps.getArrayFromResultSet();
		ProcessLogger processlogger =new ProcessLogger();
	//	line = processlogger.sortArrayIndex(line, Column.ref_index-1);
		String PROCESSID=gid.getId(pool.getRequestParameter().getProcess());
		for(int i=0;i<line.length;i++)
		{
			String Splitline[] = line[i].split("\t");
			table.clear();
			table.put("mid", Splitline[Column.pv_index-1]);
			table.put("pid", PROCESSID);
			table.put("td", "null");
			ps = new RecordSearch();
			ps.setConditionMap(table);
			String temp[]=ps.getArrayFromResultSet();
			for (int k = 0;k < temp.length; k++) 
			{	
				String splittemp[] = temp[k].split("\t");
				String value = splittemp[Column.pv_index-1];
				table.put("pid",TYPE);
				table.put("mid", splittemp[Column.pv_index-1]);
				table.put("td", "null");
				ps = new RecordSearch();
				ps.setConditionMap(table);
				String result[] = ps.getArrayFromResultSet();
				for(int j=0;j<result.length;j++)
				{
					String splitreult[] = result[j].split("\t");
					String classname = splitreult[Column.pv_index-1];
					classname =gid.getItem(classname);
					Process processboject;
					Class C;
					try 
					{
						C = Class.forName("pool.preprocess."+CapitalChar.makeFirstCharCapital(classname));
						processboject = (Process)C.newInstance();
						//processboject.setObject(this.object);
						String type = pool.getType();
						processboject.setType(type,tablename);
						Class paramlist[] = {String.class};
						Object param[] = {value};
						String methodName = "before" + CapitalChar.makeFirstCharCapital(pool.getRequestParameter().getProcess()); 
						Method method = C.getMethod(methodName,paramlist);
						method.invoke(processboject, param);
						resulttable.putAll(processboject.gethashtable());
						if(resulttable.containsKey("ErrorInput"))
						{
							break;
						}
					}
					catch (ClassNotFoundException e) 
					{
						e.printStackTrace();
					} 
					catch (InstantiationException e) 
					{	
						
					}
					catch (Exception e) 
					{
						e.printStackTrace();
					}
				}
			}
			
		}
	/*
		// type validation*************************
		if(line.length==0)
		{
			try 
			{
				validation.Validation errorvalidation;
				Class c=Class.forName("validation.Isattribute");
				errorvalidation =(validation.Validation )c.newInstance();
				errorvalidation.setDbmanager(database);
				preprocess = gid.getItem(preprocess);
				if(!errorvalidation.validate(preprocess))
				{	
					Hashtable<Object, Object>newtable = new Hashtable<Object, Object>();
					newtable.put("Errorvalidation", errorvalidation.getMessage());
					resulttable.put("ErrorInput", newtable);
				}
			}
			catch (ClassNotFoundException e1) 
			{
				e1.printStackTrace();
			} 
			catch (InstantiationException e1) 
			{
				e1.printStackTrace();
			}
			catch (Exception e1) 
			{
				e1.printStackTrace();
			}
		}
		//***************************/
	}
	
	public void setObject(Object object)
	{
		this.object =object;
		pool = (Pool ) this.object;
	}

	public Object getObject() 
	{
		return this;
	}

	public void process(String value) 
	{
		
	}

	public void setType(String type,String table) 
	{	
	}

	public Hashtable<Object, Object> gethashtable() 
	{
		return this.resulttable;
	}
}
 
